package cac.common;/*
 *@program:test-curd
 *@description:调用接口连接
 *@author: lsl
 *@Time: 2022/9/23  14:06
 */

import cac.fenjian.resource.MsGkLog.MsGkLogDTO;
import cac.fenjian.resource.mssslshelves.dao.MsGkLogDao;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;

@Service
public class HttpUtil {

    @Autowired
    private MsGkLogDao msGkLogDao;

    public String doTask() throws ParseException {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        MsGkLogDTO msGkLogDTO = new MsGkLogDTO();
        Long wmsTaskId = 1096010589L;
//        resutJSon.put("command","1");
//        resutJSon.put("taskType","1");
//        resutJSon.put("startCode","k1_3_1_1");
//        resutJSon.put("wmsTaskId",wmsTaskId);
//        resutJSon.put("palletCode","work1001");
//        resutJSon.put("roadway","XD-2");
//        resutJSon.put("endCode","1160");
        JSONObject resutJSon = new JSONObject();
        resutJSon.put("command", "1");
        resutJSon.put("taskType", "3");
        resutJSon.put("startCode", "k1_3_1_1");
        resutJSon.put("wmsTaskId", wmsTaskId);
        resutJSon.put("palletCode", "work1002");
        resutJSon.put("roadway", "XD-2");
        resutJSon.put("endCode", "k1_2_1_1");
        String params = resutJSon.toString();
        msGkLogDTO.setCommand("1");
        msGkLogDTO.setTaskType("3");
        msGkLogDTO.setWmsTaskId(wmsTaskId+"");
        msGkLogDTO.setStartCode("k1_4_1_1");
        msGkLogDTO.setEndCode("k1_2_1_1");
        msGkLogDTO.setOrderTime(DateUtil.nowTime());
//        msGkLogDao.creatLog(msGkLogDTO);
        try {
            String urlNameString = "https://372hx43205.zicp.fun/api/wcs/api/chengfei/task";
            //log.info("sendPost - {}", urlNameString);
            URL realUrl = new URL(urlNameString);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("content-type", "application/json");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(params);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            System.out.println(result);

            //log.info("recv - {}", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

}
